package com.detrum.wifidevice.receive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Parcelable;

import com.detrum.utils.LogUtils;

public class NetStateReceiver extends BroadcastReceiver {
    private final String TAG = "NetStateReceiver";
    private NetStateCall netStateCall;
    private Handler handler = new Handler();

    public NetStateReceiver() {
    }

    public NetStateReceiver(NetStateCall netStateCall) {
        this.netStateCall = netStateCall;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) {
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                NetworkInfo.State state = networkInfo.getState();
                NetworkInfo.DetailedState detailedState= networkInfo.getDetailedState();
                LogUtils.e("detailedState is "+detailedState);
                boolean isDevice = networkInfo.getExtraInfo().contains("DCAM");
                if(netStateCall==null){
                    return;
                }
                if (detailedState == NetworkInfo.DetailedState.CONNECTED) {
                    netStateCall.NetState(1);
                }else if(detailedState == NetworkInfo.DetailedState.DISCONNECTED){
                    netStateCall.NetState(0);
                }else if(detailedState == NetworkInfo.DetailedState.CONNECTING){
                    netStateCall.NetState(2);
                }else if(detailedState == NetworkInfo.DetailedState.FAILED){
                    netStateCall.NetState(3);
                }else if(detailedState == NetworkInfo.DetailedState.AUTHENTICATING){
                    netStateCall.NetState(4);
                }else if(detailedState == NetworkInfo.DetailedState.OBTAINING_IPADDR){
                    netStateCall.NetState(5);
                }
            }
        }
    }

}
